import {
  autoCreateTimetable_service,
  getTodayTimetable_service,
  TimetableType,
} from "../../graphql/timetable";
import { useLazyQuery, useMutation } from "@apollo/client";

export const useAutoCreateTimetable = () => {
  const [fun, { loading }] = useMutation(autoCreateTimetable_service);
  const createTimetable = async (date: string[]) => {
    const res = (await fun({
      variables: {
        startDay: date[0],
        endDay: date[1],
      },
      fetchPolicy: "network-only",
    })) as {
      data: { autoCreateTimetable: { message: string; code: boolean } };
    };
    return {
      message: res.data.autoCreateTimetable.message,
      code: res.data.autoCreateTimetable.code,
    };
  };
  return { createTimetable, loading };
};

export const useGetTodayTimetable = () => {
  const [fun, { loading }] = useLazyQuery(getTodayTimetable_service, {
    fetchPolicy: "network-only",
  });
  const getTodayTimetable = async (today: string) => {
    const res = (await fun({
      variables: {
        today,
      },
    })) as {
      data: {
        getTimetables: {
          message: string;
          page: { total: number };
          data: TimetableType[];
        };
      };
    };
    return {
      data: res.data?.getTimetables.data,
      page: res.data?.getTimetables.page,
      message: res.data?.getTimetables.data,
    };
  };
  return {
    getTodayTimetable,
    loading,
  };
};
